<?php
Yii::import('application.widgets.chart.AbstractTeamMonthlyChart');

class NewLoanMonthlyChart extends AbstractTeamMonthlyChart{

	public $title='New Loans Count for each month';

	protected function getMonthData($year,$month){
		$criteria=new CDbCriteria();
		$criteria->select='count(*) as count, team.name as name';
		$criteria->join='LEFT JOIN {{deal}} as deal ON deal.id=dealId LEFT JOIN {{team}} as team ON team.id=deal.teamId';		
		$criteria->group='deal.teamId';
		$nextMonth=$month+1;
		$nextYear=$year;
		if($nextMonth>12){
			$nextMonth=$nextMonth-12;
			$nextYear=$year+1;
		}
		$criteria->addBetweenCondition('t.createDate', "$year-$month-01 00:00:00", "$nextYear-$nextMonth-01 00:00:00");
		$commandBuilder= Yii::app()->db->getSchema()->getCommandBuilder();
		$command=$commandBuilder->createFindCommand(Loan::model()->getTableSchema(),$criteria);
		$row=$command->queryAll();
		$result=array('month'=>"$year-$month");
		foreach ($row as $teamRow){
			$result[$teamRow['name']]=$teamRow['count'];
		}
		return $result;
	}
}